import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../model/medicine_advice_model.dart';
import '../../net/response/medicine_advice/treatment_record_list_data.dart';
class TreatmentSheetListViewModel extends BaseListViewModel<TreatmentRecordListData>{
  TreatmentSheetListViewModel(super.context);

  ///长者姓名
  String? searchName;

  ///房间
  String? roomName;

  ///项目名称
  String? itemName;

  ///开单日期
  String? orderDate;

  ///治疗单号
  String? treatmentCode;

  ///执行状态
  PickerDataWithIdModel? handleStatus;

  var model = MedicineAdviceModel();


  @override
  Future<List<TreatmentRecordListData>> requestListData() async {
    return await model.treatmentRecord(
        page: page,
        rows: pageLength,
        prescriptionNoLike: treatmentCode,
        customerNameLike: searchName,
        bedNoLike: roomName,
        projectNameLike:itemName,
        prescriptionDateLike: orderDate,
        status: handleStatus?.pickId);
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void chooseOrderDate(){
    var format = ['yyyy','-','mm','-','dd'];
    DatePicker.showDatePicker(context,currentTime: orderDate?.toDate(),onConfirm: (date){
      orderDate = date!.toDateStr(format);
      notifyListeners();
      requestRefresh();
    });
  }

  void chooseStatus(){
    var items = [
      PickerDataWithIdModel(pickId: '0',pickName: '待执行'),
      PickerDataWithIdModel(pickId: '1',pickName: '部分完成'),
      PickerDataWithIdModel(pickId: '2',pickName: '已完成'),
    ];
    DataPickerView(context, items,currentFirstData: handleStatus,onSubmitListener: (p1,p2,p3){
      handleStatus = items[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void handleClick(TreatmentRecordListData item){

    showDialog(context: context, builder: (context){
      return Dialog(
        backgroundColor: Colors.transparent,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(height: 30,),
            CommonText('确认执行长者${item.customerName}1次“${item.projectName}”的治疗？'),
            SizedBox(height: 30,),
            Row(
              children: [
                TextButton(onPressed: (){
                  Go().pop();
                }, child: CommonText('取消',textColor: Colors.grey),
                style: ButtonStyle(
                  shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.grey)))
                ),).setWeight(1),

                SizedBox(width: 20,),
                TextButton(onPressed: (){
                  Go().pop();
                  submitApply(item.id);
                }, child: CommonText('确定',textColor: Colors.white),
                  style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                      shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.blue)))
                  ),).setWeight(1),

              ],
            ),

            SizedBox(height: 16,),
          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 10),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: 8.borderRadius
          )
        ),
      );
    });
  }

  Future<void> submitApply(dynamic id) async {
    showLoadingDialog(msg: '提交中...');
    try{
      await model.treatmentApply(id);
      hideDialog();
      requestRefresh();
    }catch(e){
      hideDialog();
      showToast(e.toNetError());
    }
  }

  void resetClick() {
    searchName = null;
    itemName = null;
    roomName = null;
    orderDate = null;
    treatmentCode = null;
    handleStatus = null;
    notifyListeners();
    requestRefresh();
  }
}